var a = getApp();

Page({
    data: {
        baseUrl: a.globalData.baseUrl,
        group: {},
        num: 1,
        totalPay: 0,
        userId: wx.getStorageSync("userId")
    },
    onLoad: function(t) {
        this.setData({
            id: t.id
        });
        var e = a.globalData.urlBase + a.globalData.urlDetail;
        this.getinitData(e);
    },
    getinitData: function(a) {
        var t = this;
        wx.request({
            url: a,
            data: {
                tuanId: t.data.id
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                0 != e.code ? wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }) : t.setData({
                    group: e.tuan,
                    totalPay: e.tuan.price
                });
            }
        });
    },
    mulNum: function() {
        var a = this, t = a.data.num, e = a.data.group.price;
        t <= 1 ? t = 1 : t -= 1, a.setData({
            num: t,
            totalPay: t * e
        });
    },
    addNum: function() {
        var a = this, t = a.data.num, e = a.data.group.price;
        t >= 99 ? t = 99 : t += 1, a.setData({
            num: t,
            totalPay: t * e
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    toPay: function() {
        this.createOrder();
    },
    createOrder: function() {
        var t = this, e = t.data.id, n = t.data.num, o = a.globalData.urlBase + a.globalData.urlAddTuanOrder;
        wx.request({
            url: o,
            data: {
                groupId: e,
                num: n,
                userId: t.data.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                0 != e.code ? wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }) : t.getPayData(e.msg);
            }
        });
    },
    getPayData: function(t) {
        var e = this, n = a.globalData.urlBase + a.globalData.urlPayData;
        wx.request({
            url: n,
            data: {
                orderId: t,
                userId: e.data.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var t = JSON.parse(a.data);
                0 != t.code ? wx.showToast({
                    title: t.msg,
                    icon: "none",
                    duration: 2e3
                }) : e.wxPay(t);
            }
        });
    },
    wxPay: function(a) {
        wx.requestPayment({
            timeStamp: a.timeStamp,
            nonceStr: a.nonceStr,
            package: "prepay_id=" + a.totalId,
            signType: "MD5",
            paySign: a.sign,
            success: function(a) {
                wx.showToast({
                    title: "支付完成",
                    icon: "none",
                    duration: 2e3
                });
            },
            fail: function(a) {
                wx.showToast({
                    title: "支付失败",
                    icon: "none",
                    duration: 2e3
                });
            },
            complete: function(a) {
                wx.navigateTo({
                    url: "../order/MyOrder"
                });
            }
        });
    },
    onShareAppMessage: function() {}
});